//全局function
$(function(){
	//初始化TAB
	$('#tab').tabs({   
	      border:false,
	      plain:true,
	      onSelect:function(title){
	    	  if(title == '运营后台菜单'){
	    		  $('#backendWin').show();
	    		  $('#merchantWin').hide();
	    		  $("#queryTypeInt").val(0);
	    	  }else if(title == '渠道平台菜单'){
	    		  $('#merchantWin').show();
	    		  $('#backendWin').hide();
	    		  $("#queryTypeInt").val(1);
	    	  }
	      }
	 });
	$('#backendMenuTree').tree({    
	    url: mainDomain+'/menu/findSubMenuById.action',
	    animate:true,
	    method:'get',
	    dnd:true,
		onBeforeExpand:function(node) {
		    $('#backendMenuTree').combotree("tree").tree("options").url = mainDomain+"/menu/findSubMenuById.action?categoryId=" + node.id;
		},
	    formatter:function(node){
	    	var opts=[];
	    	if(0==node.id){
	    		opts.push('<span style="font-size:14px;">'+node.text+'</span>');
	    		opts.push('<span title="新增子菜单" style="margin-left:10px;" class="tree-icon tree-file icon-add" onclick="addSubCategory(0)"></span>');
	    	}else{
	    		opts.push('<span style="font-size:13px;">'+node.text+'</span>');
	    		var category=node.attributes;
	    		if(category !=undefined && category.categoryLevel<cagegoryGlobal.maxLevel){
	    			opts.push('<span title="新增子菜单" style="margin-left:10px;" class="tree-icon tree-file icon-add" onclick="addSubCategory('+node.id+')"></span>');
	    		}
	    		opts.push('<span title="编辑子菜单" style="margin-left:5px;" class="tree-icon tree-file icon-edit" onclick="updateCategory('+node.id+')"></span>');
	    		opts.push('<span title="删除菜单" style="margin-left:5px;" class="tree-icon tree-file icon-remove" onclick="delCategory('+node.id+')"></span>');
	    	}
			return opts.join('');;
		},
	    loadFilter: function(data,parent){    
	    	var treeJsons=[];
	    	//数据格式转换
	    	if(!$.isEmptyObject(data)){
	    		$.each(data,function(i,v){
	    			var nodeJson={};
	    			nodeJson.id=v.id;
	    			nodeJson.text=v.categoryName;
	    			nodeJson.state='closed';
	    			nodeJson.attributes=v;
	    			if(v.categoryLevel==cagegoryGlobal.maxLevel){
	    				nodeJson.children=[];
	    			}
	    			treeJsons[i]=nodeJson;
	    			
	    		});
	    	}
	    	//第一级进入页面加 所有类别
	    	if(null == parent){
	    		var datas=[];
	    		var root={
	    				id:0,
		    			text: '所有菜单',
		    			state: 'open',
		    			children:treeJsons
		    	};
	    		datas.push(root);
	    		return datas;
	    	}
	    	return treeJsons;
	    }    
	});  

});
